home *** CD-ROM | disk | FTP | other *** search
/ Delphi Developer's Kit 1996 / Delphi Developer's Kit 1996.iso / power / wfc007.000 / src / talksock.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-22  |  3.3 KB  |  161 lines

  1. #include <wfc.h>
  2. #pragma hdrstop
  3.  
  4. /*
  5. ** Author: Samuel R. Blackburn
  6. ** CI$: 76300,326
  7. ** Internet: sammy@sed.csc.com
  8. **
  9. ** You can use it any way you like.
  10. */
  11.  
  12. #if defined( _DEBUG )
  13. #undef THIS_FILE
  14. static char BASED_CODE THIS_FILE[] = __FILE__;
  15. #endif
  16.  
  17. CTalkingSocket::CTalkingSocket()
  18. {
  19.    m_Initialize();
  20. }
  21.  
  22. CTalkingSocket::CTalkingSocket( const CString& a_host_name, const CString& p_name )
  23. {
  24.    m_Initialize();
  25.  
  26.    SetAddress( a_host_name );
  27.    SetPort( p_name );
  28. }
  29.  
  30. CTalkingSocket::CTalkingSocket( const CString& address, const short p_number )
  31. {
  32.    m_Initialize();
  33.  
  34.    SetAddress( address );
  35.    SetPort( p_number );
  36. }
  37.  
  38. CTalkingSocket::~CTalkingSocket()
  39. {
  40.    TRACE( "Destroying a TALKING_SOCKET object\n" );
  41. }
  42.  
  43. void CTalkingSocket::Dump( CDumpContext &dump_context ) const
  44. {
  45.    CObject::Dump( dump_context );
  46. }
  47.  
  48. void CTalkingSocket::m_Initialize( void )
  49. {
  50.    ASSERT_VALID( this );
  51.  
  52.    TRACE( "Initializing a TALKING_SOCKET object\n" );
  53. }
  54.  
  55. /*
  56. ** The Socket manipulation routines
  57. */
  58.  
  59. BOOL CTalkingSocket::Open( void )
  60. {
  61.    ASSERT_VALID( this );
  62.  
  63.    if ( Address.IsEmpty() )
  64.    {
  65.       /*
  66.       ** We don't have an address
  67.       */
  68.  
  69.       return( FALSE );
  70.    }
  71.  
  72.    SOCKADDR_IN server_address;
  73.  
  74.    /*
  75.    ** Create the socket
  76.    */
  77.  
  78.    m_SocketID = ::socket( AF_INET, SOCK_STREAM, 0 );
  79.  
  80.    if ( m_SocketID == INVALID_SOCKET )
  81.    {
  82.       return( FALSE );
  83.    }
  84.  
  85.    /*
  86.    ** Now fill in a socket address structure with the necessary information about the remote
  87.    ** server node (remote node IP address and port for incoming connections) and attempt to
  88.    ** connect to the server. This connect call will block until the remote server has accepted
  89.    ** the connection or the connection request times out.
  90.    */
  91.  
  92.    server_address.sin_family      = AF_INET;
  93.    server_address.sin_port        = m_PortNumberInNetworkByteOrder;
  94.    server_address.sin_addr.s_addr = ::inet_addr( (const char *) Address );
  95.  
  96.    int connection_status = 0;
  97.  
  98.    connection_status = ::connect( m_SocketID, (LPSOCKADDR) &server_address, sizeof( server_address ) );
  99.  
  100.    if ( connection_status == SOCKET_ERROR )
  101.    {
  102.       m_ErrorCode = ::WSAGetLastError();
  103.  
  104.       TRACE( "TALKING_SOCKET::Open(), connect failed at line %d of %s error #%d, address = %s, port number %d\n",
  105.              __LINE__, 
  106.              __FILE__,
  107.              m_ErrorCode,
  108.              (const char *) Address,
  109.              ::ntohs( m_PortNumberInNetworkByteOrder ) );
  110.  
  111.       Close();
  112.       return( FALSE );
  113.    }
  114.  
  115.    m_hFile = (UINT) m_SocketID;
  116.     
  117.    return( TRUE );
  118. }
  119.  
  120. #pragma warning( disable : 4100 )
  121.  
  122. BOOL CTalkingSocket::Open( const char * a, UINT port_number, CFileException *perror )
  123. {
  124.    ASSERT_VALID( this );
  125.    ASSERT( a != NULL );
  126.  
  127.    if ( a == NULL )
  128.    {
  129.       m_ErrorCode = ERROR_INVALID_PARAMETER;
  130.       return( FALSE );
  131.    }
  132.  
  133.    SetAddress( a );
  134.    SetPort( (short) port_number );
  135.  
  136.    return( Open() );
  137. }
  138.  
  139. #pragma warning( default : 4100 )
  140.  
  141. BOOL CTalkingSocket::Open( const CString& a, const short p )
  142. {
  143.    ASSERT_VALID( this );
  144.    ASSERT( p > 0 );
  145.  
  146.    SetAddress( a );
  147.    SetPort( p );
  148.  
  149.    return( Open() );
  150. }
  151.  
  152. BOOL CTalkingSocket::Open( const CString& _host_name, const CString& _port_name )
  153. {
  154.    ASSERT_VALID( this );
  155.  
  156.    SetAddress( _host_name );
  157.    SetPort( _port_name );
  158.  
  159.    return( Open() );
  160. }
  161.